package data.nanjing.expo.ctrls;

import data.nanjing.expo.common.ExpoConstants;
import data.nanjing.expo.common.DateTimeUtil;
import data.nanjing.expo.common.GroupUtil;
import data.nanjing.expo.common.Util;
import data.nanjing.expo.models.domain.dto.exhibition.ExpoScreenDto;
import data.nanjing.expo.services.WebSourceManageService;
import data.nanjing.expo.models.domain.*;
import data.nanjing.expo.services.*;
import data.nanjing.expo.models.domain.dto.ActivityInfoDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.*;

@Controller
@RequestMapping("/")
public class RenderCtrl extends BaseCtrl {
    @Autowired
    ExpoService exhibitionService;
    @Autowired
    WebSourceManageService webSourceManageService;

    @Autowired
    private ActivityInfoService activityInfoService;

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private ActivityTagInfoService activityTagInfoService;

    @Autowired
    private TradeRequireService requireService;

    @Autowired
    private FileManageService fileManageService;

    @Autowired
    private TradeRequireService tradeRequireService;


    /***************************************展览中心*******************************************/

    /**
     * 分页显示展会二级页面
     * @return
     */
    @GetMapping("exhibition")
    public ModelAndView getExhibitionList(@RequestParam(value = "page",required = false)Integer page,
                                          @RequestParam(value = "limit",required = false) Integer limit){

        Map<String, Object> map =  exhibitionService.getExhibitionForRender(page,limit);
        System.out.print(map);
        map.put("exhibitionSelect",true);
        map.put("banner",webSourceManageService.getWebSourceManage("exhibition"));
        return this.render("exhibition/index",map);
    }

    /**
     * 根据展会id获取展会详情
     * @param es
     * @param request
     * @return
     */
    @GetMapping("/exhibition/expoDetail")
    public ModelAndView getExpoDetail(ExpoScreenDto es, HttpServletRequest request){
        Map<String,Object> map =  exhibitionService.getExhibitionDetailForRender(es,request);
        return this.render("exhibition/exhibitionDetail",map);
    }

    /**
     * 参展商详情
     * @param id
     * @return
     */
    @GetMapping("/exhibition/exhibitor/{id}")
    public ModelAndView exhibitor(@PathVariable Integer id){
        Map<String,Object> map = exhibitionService.getExpoParticipant(id);
        System.out.print(map);
        return this.render("exhibition/exhibitorDetail",map);
    }
    /*****************************************************************************************/




     /**
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value="/admin/**", method=RequestMethod.GET)
    public ModelAndView renderAdmin(Map<String, Object> model, HttpServletRequest request) {
        Map<String, Object> data = new HashMap<>();
        data.put("title", "INDEX");
        return this.render("admin", data);
    }

    @RequestMapping(value="/console/**", method=RequestMethod.GET)
    public ModelAndView renderConsole(Map<String, Object> model, HttpServletRequest request) {
        Map<String, Object> data = new HashMap<>();
        data.put("title", "用户中心");
        return this.render("console", data);
    }
    @RequestMapping(value="/sign/**", method=RequestMethod.GET)
    public ModelAndView renderSign(Map<String, Object> model, HttpServletRequest request) {
        Map<String, Object> data = new HashMap<>();
        data.put("title", "登陆|注册");
        return this.render("sign", data);
    }
    @RequestMapping(value="/", method=RequestMethod.GET)
    public ModelAndView renderIndex(Map<String, Object> model, HttpServletRequest request) {
        Map<String,Object> data=exhibitionService.getExhibitionForRender(1,3);
        data.put("banner",webSourceManageService.getWebSourceManage("index"));
        data.put("activity",activityInfoService.selectHotActivityList());
        data.put("request",tradeRequireService.selectHotRequireList());
        return this.render("index", data);
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request) {
        this.removeUserFromSession(request);
        return "redirect:/";
    }


    /*******活动中心直出部分******************************************************************************************/
    //活动首页
    @GetMapping("/activity")
    public ModelAndView renderActivity(@RequestParam(name = "industry", required = false) String industry,
                                       @RequestParam(name = "fee", required = false) String fee,
                                       @RequestParam(name = "time", required = false) String time,
                                       @RequestParam(name = "pageNumber", defaultValue = "1")Integer pageNumber,
                                       @RequestParam(name = "pageSize", defaultValue = "8")Integer pageSize,
                                       HttpServletRequest request) {
        Map<String, Object> data = new HashMap<>();
        List<SysConfig> list = sysConfigService.findAll();
        Map<String, List<SysConfig>> map = GroupUtil.groupList(list);
        List<SysConfig> feeConfig = map.get(ExpoConstants.ACTIVITY_FEE);
        List<SysConfig> industryConfig = map.get(ExpoConstants.ACTIVITY_INDUSTRY);
        List<SysConfig> timeConfig = map.get(ExpoConstants.ACTIVITY_TIME);
        Date datetime = new Date();
        if (fee == null || "".equals(fee)) {
            fee = "any";
        }
        if (industry == null || "".equals(industry)) {
            industry = "不限";
        }
        if (time == null || "".equals(time) || "any".equals(time)) {
            time = "any";
            datetime = null;
        }else {
            datetime = DateTimeUtil.dateAddDays(datetime, Integer.parseInt(time), Calendar.DATE);
        }
        String url = request.getRequestURL().toString();
        if (request.getQueryString() != null) {
            url += "?" + request.getQueryString();
        }

        for (SysConfig ts : feeConfig) {
            //build url
            ts.setUrl(Util.buildQuery(url, "fee", ts.getCode()));
            if (ts.getCode().equals(fee)) {
                ts.setSelected(true);
            } else {
                ts.setSelected(false);
            }
        }

        for (SysConfig ts : industryConfig) {
            ts.setUrl(Util.buildQuery(url, "industry", ts.getName()));
            if (ts.getName().equals(industry)) {
                ts.setSelected(true);
            } else {
                ts.setSelected(false);
            }
        }

        for (SysConfig ts : timeConfig) {
            ts.setUrl(Util.buildQuery(url, "time", ts.getCode()));
            if (ts.getCode().equals(time)) {
                ts.setSelected(true);
            } else {
                ts.setSelected(false);
            }
        }
        List<WebSourceManage> webSourceManages=webSourceManageService.getWebSourceManage("activity");
        data.put("banner", webSourceManages);
        data.put("fee", feeConfig);
        data.put("industry", industryConfig);
        data.put("time", timeConfig);
        List<ActivityTagInfo> tagInfos = activityTagInfoService.selectHotTagInfoList();
        data.put("tag", tagInfos);
        //最新活动
        List<ActivityInfo> latestActivity = activityInfoService.selectLatestActivityInfoList();
        data.put("latestActivity", latestActivity);
        //查询列表
        List<ActivityInfo> activityList = activityInfoService.selectActivityList(industry, fee, datetime, pageNumber, pageSize);
        if(activityList.size()!=0){
            data.put("list", activityList);
        }
        data.put("activitySelect",true);
        return this.render("activity/index", data);
    }
    //活动中心详情
    @GetMapping("/activity/detail/{activityId}")
    public ModelAndView renderActivityDetail(@PathVariable("activityId") Integer activityId) {
        Map map = new HashMap();
        ActivityInfoDto activityInfoDto = activityInfoService.selectActivityDetailInfoById(activityId);
        List<ActivityInfo> activityInfos = activityInfoService.selectRecommendActivity();
        map.put("detail", activityInfoDto);
        map.put("recommend", activityInfos);
        return this.render("activity/activityDetail", map);
    }
    /*******需求直出部分*************************************************************************************/

    //需求详情
    @GetMapping("/require/detail/{requireId}")
    public ModelAndView renderRequireDetail(@PathVariable("requireId") Integer requireId) {
        Map<String, Object> map = new HashMap<>();
        TradeRequire require = requireService.selectRequireById(requireId);
        List<FileManage> fileList = fileManageService.getFileManage(requireId, ExpoConstants.REQUIRE_FILE);
        map.put("detail", require);
        map.put("file", fileList);
        return this.render("", map);

    }
    @GetMapping("/require")
    public ModelAndView renderRequire(HttpServletRequest request) {
        Map<String, Object> data = new HashMap<>();
        return this.render("trade/requireIndex", data);
    }

}
